/*
 *  PhoneNumberValidator.cs
 *  
 *  Validates whether the user input is a valid phone number, and auto-
 *  corrects to the standard format.
 *  
 *  Copyright (c) 2009, Richard Bao Labs.
 *  
 *  The contents of this file are subject to the Mozilla Public License
 *  Version 1.1 (the "License"); you may not use this file except in
 *  compliance with the License. You may obtain a copy of the License at
 *  
 *  http://www.mozilla.org/MPL/
 *  
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 *  the License for the specific language governing rights and limitations
 *  under the License.
 *  
 *  The Original Code is RichControlLibrary.
 *  
 *  The Initial Developer of the Original Code is Richard Bao Labs.
 *  All Rights Reserved.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace RichControlLibrary
{
    /// <summary>
    /// Validates whether the user input is a valid phone number, and auto-corrects to the standard
    /// format.
    /// </summary>
    /// <remarks>
    /// General telephone format. It accepts +1 (605) 123-4567 or +1-605-123-4567
    /// or +1 605 123 4567 or 12345678 or 1234 5678 x 1234.
    /// </remarks>
    public sealed class PhoneNumberValidator : RegexValidator
    {
        /// <summary>
        /// Returns a <see cref="Regex"/> instance as the validating regular expression.
        /// </summary>
        /// <returns>A <see cref="Regex"/> used to validate whether the user input is a valid phone
        /// number.</returns>
        protected override Regex GetValidatingRegex()
        {
            string regexStr =
                @"^" +
                @"(?<country>\+[0-9]{1,5})?" +
                @"\s*" +
                @"(?<area>\([0-9]+\))?" +
                @"\s*" +
                @"(?<number>" +
                    @"([0-9]{5,})" +
                @"|" +
                    @"(" +
                        @"[0-9]{2,}" +
                        @"((\s+[0-9]{2,})|(\-[0-9]{2,}))+" +
                    @")" +
                @")" +
                @"\s*" +
                @"(x\s*(?<ext>[0-9]+))?" +
                @"$";

            return new Regex(regexStr);
        }

        /// <summary>
        /// Gets a <see cref="System.Boolean"/> value that indicates whether the auto formatting feature
        /// is supported by this validator.
        /// </summary>
        public override bool SupportsAutoFormatting
        {
            get { return true; }
        }

        /// <summary>
        /// Gets the auto formatted phone number.
        /// </summary>
        /// <param name="rawText">The raw text string entered by user.</param>
        /// <param name="match">The matching result of the validating regular expression.</param>
        /// <returns>The auto formatted phone number.</returns>
        protected override string GetAutoFormattedText(string rawText, Match match)
        {
            StringBuilder sb = new StringBuilder();

            if (match.Groups["country"].Success)
            {
                sb.Append(match.Groups["country"].Value);
                sb.Append(" ");
            }

            if (match.Groups["area"].Success)
            {
                sb.Append(match.Groups["area"].Value);
                sb.Append(" ");
            }
            
            sb.Append(match.Groups["number"].Value.Replace("-", "").Replace(" ", ""));

            if (match.Groups["ext"].Success)
            {
                sb.Append(" x " + match.Groups["ext"].Value);
            }

            return sb.ToString();
        }
    }
}
